package com.fescoservice.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.fescoservice.exception.FSException;
import com.fescoservice.model.CategoryImage;
import com.fescoservice.model.CategoryImageId;

/**
 * A data access object (DAO) providing persistence and search support for
 * CategoryImage entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.fescoservice.dao.CategoryImage
 * @author MyEclipse Persistence Tools
 */

public class CategoryImageDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(CategoryImageDAO.class);
	// property constants
	public static final String TITLE = "title";
	public static final String URL = "url";
	public static final String USER_ID = "userId";
	public static final String LOCALE = "locale";
	public static final String STATUS = "status";

	protected void initDao() {
		// do nothing
	}

	public void save(CategoryImage transientInstance) {
		log.debug("saving CategoryImage instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public Long countCategoryImages(final String sql ,final CategoryImageId id) throws FSException{
		log.debug("finding Category instance by example");
		try {
			Long count = (Long) getHibernateTemplate().execute(
					   new HibernateCallback() {
					   public Object doInHibernate(Session session)
					   throws HibernateException, SQLException {
						   SQLQuery q = session.createSQLQuery(sql);
						   q.setString("boardCode1" , id.getBoardCode1());   
						   q.setString("boardCode2" , id.getBoardCode2());   
						   q.setString("boardCode3" , id.getBoardCode3()); 
						  return q.addScalar( "cnt" , Hibernate.LONG).uniqueResult();
					   }
					 } );
			return count;
		} catch (Exception re) {
			log.error("count categories failed", re);
			throw new FSException("FS_ERROR_COUNTCATGORY",re.getMessage());
		}
	}

	public void delete(CategoryImage persistentInstance) {
		log.debug("deleting CategoryImage instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void delCategoryImages(final String hql,final String boardcode1,final String boardcode2,final String boardcode3) throws FSException{
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws SQLException,
					HibernateException {
					Query q = session.createQuery(hql); 
					   q.setString("boardCode1" , boardcode1);   
					   if(!"FM0000".equals(boardcode2)){
						   q.setString("boardCode2" , boardcode2); 					   
					   }
					   if(!"FB0000".equals(boardcode3)){
						   q.setString("boardCode3" , boardcode3); 	
					   }
					q.executeUpdate();
				return null;
			}
		});
	}
	

	public CategoryImage findById(CategoryImageId id) {
		log.debug("getting CategoryImage instance with id: " + id);
		try {
			CategoryImage instance = (CategoryImage) getHibernateTemplate()
					.get("com.fescoservice.model.CategoryImage", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(CategoryImage instance) {
		log.debug("finding CategoryImage instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CategoryImage instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from CategoryImage as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByUrl(Object url) {
		return findByProperty(URL, url);
	}

	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findByLocale(Object locale) {
		return findByProperty(LOCALE, locale);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findAll() {
		log.debug("finding all CategoryImage instances");
		try {
			String queryString = "from CategoryImage";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CategoryImage merge(CategoryImage detachedInstance) {
		log.debug("merging CategoryImage instance");
		try {
			CategoryImage result = (CategoryImage) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CategoryImage instance) {
		log.debug("attaching dirty CategoryImage instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CategoryImage instance) {
		log.debug("attaching clean CategoryImage instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CategoryImageDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (CategoryImageDAO) ctx.getBean("CategoryImageDAO");
	}
}